﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

#include<QFileDialog>
#include<QDebug>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    sourcePath = nullptr;
    directPath = nullptr;

    fileOperator = nullptr;

    ui->setupUi(this);

    connect(ui->resetBtn,&QPushButton::clicked,[=](){

        ui->sourceInput->setText("");
        ui->directInput->setText("");
        sourcePath = nullptr;
        directPath = nullptr;

    });


    connect(ui->selectSource,&QPushButton::clicked,[=](){

        QString msg = QFileDialog::getExistingDirectory(this,"Choose Source Directory","/");
        ui->sourceInput->setText(msg);
        sourcePath = msg;
    });

    connect(ui->selectDirect,&QPushButton::clicked,[=](){
        QString msg = QFileDialog::getExistingDirectory(this,"Choose Direct Directory","/");
        ui->directInput->setText(msg);
        directPath = msg;
    });

    connect(ui->startBtn,&QPushButton::clicked,[=](){

        if(sourcePath == nullptr || directPath == nullptr)
        {
            qDebug()<<"未写完目录";
            return;
        }

        ui->logRecord->setText("源目录："+sourcePath+"\n");
        ui->logRecord->setText(ui->logRecord->toPlainText() + "目标目录："+directPath+"\n");

        if(fileOperator == nullptr)
        {
            fileOperator = new CopyOperator(sourcePath.toUtf8().data(),directPath.toUtf8().data());

            void(SignalTemp::*sendStatus)(QString,QString) = &SignalTemp::sendFileStatus;
            connect(fileOperator->signalTemp,sendStatus,this,&MainWindow::ChangeLogSlot);
//            fileOperator->SetFileInfoCallBack(std::bind(&MainWindow::ChangeLog,this,std::placeholders::_1,std::placeholders::_2));
        }

        fileOperator->start();
    });

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::ChangeLogSlot(const QString FileName, const QString Msg)
{
    QString orgTxt = ui->logRecord->toPlainText();
    orgTxt.append("["+FileName+"] "+Msg+"\n");
    ui->logRecord->setText(orgTxt);
}

void MainWindow::ChangeLog(const QString FileName, const QString Msg)
{
    QString orgTxt = ui->logRecord->toPlainText();
    orgTxt.append("["+FileName+"] "+Msg+"\n");
    ui->logRecord->setText(orgTxt);
}

